package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.Account;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.MenuButtonInfoMapper;
import com.zc.angelica.entity.MenuButtonInfo;
import com.zc.angelica.service.MenuButtonInfoService;
import com.zc.angelica.utils.SecurityUtil;
import org.springframework.stereotype.Service;

/**
 * 菜单按钮关系表(MenuButtonInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-04-08 22:02:21
 */
@Service
public class MenuButtonInfoServiceImpl extends ServiceImpl<MenuButtonInfoMapper, MenuButtonInfo> implements MenuButtonInfoService {

    @Override
    public ResponseData bindingButton(MenuButtonInfo menuButtonInfo) {
        if (menuButtonInfo.getButtonId() == null || menuButtonInfo.getMenuId() == null || menuButtonInfo.getButtonCode() == null || menuButtonInfo.getMenuCode() == null) {
            return ResponseData.error("请求参数不能为空");
        }
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo != null) {
            QueryWrapper<MenuButtonInfo> menuButtonInfoQueryWrapper = new QueryWrapper<>();
            menuButtonInfoQueryWrapper.eq("account_id", accountInfo.getId()).eq("button_code", menuButtonInfo.getButtonCode()).eq("menu_code", menuButtonInfo.getMenuCode());
            long buttonCount = this.count(menuButtonInfoQueryWrapper);
            if (buttonCount > 0) {
                return ResponseData.error("请勿重复绑定");
            }
            menuButtonInfo.setAccountId(accountInfo.getId());
            boolean save = this.save(menuButtonInfo);
            if (save) {
                return ResponseData.success("绑定成功");
            }
            return ResponseData.error("绑定失败");
        }
        return ResponseData.error("没有查询到用户信息，请重新登录再试");
    }

    @Override
    public ResponseData unbindingButton(Long menuId, Long buttonId) {
        QueryWrapper<MenuButtonInfo> menuButtonInfoQueryWrapper = new QueryWrapper<>();
        menuButtonInfoQueryWrapper.eq("menu_id", menuId).eq("button_id", buttonId);
        boolean remove = this.remove(menuButtonInfoQueryWrapper);
        if (remove) {
            return ResponseData.success("解绑成功");
        }
        return ResponseData.error("解绑失败");
    }
}

